﻿package frEngine.loaders.resource.info
{
    import com.gengine.gc.*;
    import flash.utils.*;
    import frEngine.core.*;
    import frEngine.shader.filters.*;

    public class MeshInfo extends Info3dBase
    {
        public var meshName:String;
        public var defaultMaterial:String;
        public var vertexNum:uint;
        public var faceNum:uint;
        public var version:uint;
        public var useAlpha:Boolean;
        private var _surf:FrSurface3D;

        public function MeshInfo(param1:Object)
        {
            super(param1);
            return;
        }// end function

        public function get surface3d() : FrSurface3D
        {
            return this._surf;
        }// end function

        override public function dispose() : void
        {
            if (this._surf)
            {
                this._surf.clear();
            }
            super.dispose();
            return;
        }// end function

        override public function set data(param1:Object) : void
        {
            super.data = param1;
            _byteArray.position = 0;
            _byteArray.endian = Endian.LITTLE_ENDIAN;
            var _loc_2:* = _byteArray.readUnsignedShort();
            this.version = _byteArray.readUnsignedShort();
            if (checkIsCompressMesh(_loc_2, this.version))
            {
                _byteArray.position = 0;
                _byteArray.uncompress();
                _loc_2 = _byteArray.readUnsignedShort();
                this.version = _byteArray.readUnsignedShort();
            }
            var _loc_3:* = Boolean(_loc_2 & 1);
            this.useAlpha = Boolean(_loc_2 & 16);
            if (!this._surf)
            {
                this._surf = new FrSurface3D(this.name);
                this._surf.targetInfo = this;
            }
            else
            {
                this._surf.clear();
            }
            this.parserNormaMesh(this._surf, _loc_3, this.useAlpha);
            return;
        }// end function

        private function parserNormaMesh(param1:FrSurface3D, param2:Boolean, param3:Boolean) : void
        {
            this.meshName = readString(_byteArray);
            param1.name = this.meshName;
            this.defaultMaterial = readString(_byteArray);
            param2 = _byteArray.readByte() == 1 || param2;
            this.vertexNum = _byteArray.readUnsignedShort();
            this.faceNum = _byteArray.readUnsignedShort();
            var _loc_4:* = new GByteArray();
            _loc_4.endian = Endian.LITTLE_ENDIAN;
            var _loc_5:* = new GByteArray();
            _loc_5.endian = Endian.LITTLE_ENDIAN;
            var _loc_6:int = 2;
            var _loc_7:* = param3 ? (4) : (3);
            _loc_6 = _loc_6 + _loc_7;
            var _loc_8:* = _loc_6 + 3;
            _loc_6 = _loc_6 + 3;
            _byteArray.readBytes(_loc_4, 0, this.vertexNum * _loc_6 * 4);
            _byteArray.readBytes(_loc_5, 0, this.faceNum * 3 * 2);
            if (param2)
            {
                param1.addVertexData(FilterName_ID.POSITION_ID, _loc_7, false, null);
                param1.addVertexData(FilterName_ID.UV_ID, 2, false, null);
                param1.addVertexData(FilterName_ID.NORMAL_ID, 3, false, null);
            }
            else
            {
                param1.addVertexData(FilterName_ID.POSITION_ID, _loc_7, false, null);
                param1.addVertexData(FilterName_ID.UV_ID, 2, false, null);
            }
            param1.getVertexBufferByNameId(FilterName_ID.POSITION_ID).vertexBytes = _loc_4;
            param1.indexBytes = _loc_5;
            return;
        }// end function

        public static function checkIsCompressMesh(param1:uint, param2:uint) : Boolean
        {
            var _loc_3:Boolean = false;
            if (param1 == 0 || param1 == 4096 || param1 & 1 || param1 & 16)
            {
                if (param2 == 1)
                {
                    _loc_3 = true;
                }
                else
                {
                    _loc_3 = false;
                }
            }
            else
            {
                _loc_3 = false;
            }
            return !_loc_3;
        }// end function

        public static function readString(param1:ByteArray) : String
        {
            var _loc_2:String = null;
            var _loc_6:int = 0;
            var _loc_3:int = 0;
            var _loc_4:* = param1.position;
            var _loc_5:int = 500;
            while (_loc_3 < _loc_5)
            {
                
                _loc_2 = param1.readMultiByte(1, "gb2312");
                if (_loc_2 == "\\")
                {
                    _loc_6 = param1.position - _loc_4 - 1;
                    param1.position = _loc_4;
                    _loc_2 = param1.readMultiByte(_loc_6, "gb2312");
                    param1.position = param1.position + 2;
                    break;
                }
                _loc_3++;
            }
            if (_loc_3 == _loc_5)
            {
                throw new Error("3dmax输出有误，字符串没有加\\");
            }
            return _loc_2;
        }// end function

    }
}
